﻿Public Class frmLocPhieu
    Private Sub frmLocPhieu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ToolStripStatusLabel1.Text = ""
            Init()
        Catch ex As Exception
            ShowError(ex.ToString())
        End Try
    End Sub

    Public Sub Huy()
        'Reset lai bien toan cuc
        myNX = ""
        myLoaiMH = ""
        myLK = ""
        mySoPhieu = ""
        myDVGN = ""
        myTCNX = ""
        myBCTH = -1
        'myTCSL = False
        myHopDong = -1
        myKyQT = -1
        myNGAY = DateNull()
        myNGAYTo = DateNull()
        mySqlDK = ""
        myShowDK = ""

        'Reset lai Form
        Dim arrLoaiKho As ArrayList
        arrLoaiKho = CDoiTuongs.GetDoiTuongList(enumDoiTuong.LOAIKHO)
        Me.lbxLoaiKho.Items.Clear()
        For Each objLoaiKho As CDoiTuong In arrLoaiKho
            Me.lbxLoaiKho.Items.Add(objLoaiKho)
        Next

        txtSoPhieu.Text = ""
        txtDVGN.Text = ""
        txtTCN.Text = ""
        txtTCX.Text = ""
        txtSoHD.Text = ""
        chkGiaTri.Checked = False
        chkTinhTrang.Checked = False
        chkLoaiMH.Checked = False
        chkKyQT.Checked = False
        rdoTatCa.Checked = True
    End Sub

    Public Sub Init()
        'Lay danh sach Loai Thuoc
        If cmbLoaiMH.DataSource Is Nothing Then
            cmbLoaiMH.DisplayMember = "TenLoaiThuoc"
            cmbLoaiMH.ValueMember = "MaLoaiThuoc"
            cmbLoaiMH.DataSource = CLoaiThuocs.GetLoaiThuocListByMaCha(CLoaiThuocs.GetLoaiThuocByTen("Kho 706").MaLoaiThuoc)
        End If

        'Lay danh muc ky quyet toan
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        cmbKyQT.DisplayMember = "tenkyqt"
        cmbKyQT.ValueMember = "id"
        cmbKyQT.DataSource = ds.Tables(0)

        LoadDK()

        If myNX = "" Then
            rdoTatCa.Checked = True
        ElseIf myNX = "N" Then
            rdoPhieuNhap.Checked = True
        Else
            rdoPhieuXuat.Checked = True
        End If

        'Lisbox Loai Kho
        Dim arrLoaiKho As ArrayList
        arrLoaiKho = CDoiTuongs.GetDoiTuongList(enumDOITUONG.LOAIKHO)
        Me.lbxLoaiKho.Items.Clear()
        For Each objLoaiKho As CDoiTuong In arrLoaiKho
            Me.lbxLoaiKho.Items.Add(objLoaiKho, ChkValue(objLoaiKho.KH))
        Next

        If myLoaiMH <> "" Then
            chkLoaiMH.Checked = True
            'cmbLoaiMH.Text = CLoaiThuocs.GetLoaiThuocByMa(myLoaiMH).TenLoaiThuoc
            cmbLoaiMH.SelectedValue = myLoaiMH
        Else
            chkLoaiMH.Checked = False
            cmbLoaiMH.Enabled = False
        End If

        If myKyQT > 0 Then
            chkKyQT.Checked = True
            cmbKyQT.SelectedValue = myKyQT
        Else
            chkKyQT.Checked = False
            cmbKyQT.Enabled = False
        End If

        txtSoPhieu.Text = mySoPhieu
        txtDVGN.Text = CDoiTuongs.GetNameByMa(enumDOITUONG.DONVI, myDVGN)
        Dim startIndex As Integer = myTCNX.IndexOf(";")
        If startIndex > 0 Then
            txtTCN.Text = CDoiTuongs.GetNameByMa(enumDOITUONG.TINHCHATNHAP, myTCNX.Substring(0, startIndex))
            txtTCX.Text = CDoiTuongs.GetNameByMa(enumDOITUONG.TINHCHATXUAT, myTCNX.Substring(startIndex + 1))
        End If
        txtSoHD.Text = CHopDongs.GetSoHDByHopDong_ID(myHopDong)

        'Load Tinh Trang Phieu
        Dim arys As ArrayList = CBCTHs.GetTinhTrangPhieus()
        'Dim i As Integer = -1
        Me.cmbTinhTrang.DataSource = arys
        Me.cmbTinhTrang.DisplayMember = "DESC"
        Me.cmbTinhTrang.ValueMember = "BCTH"

        'cmbTinhTrang.SelectedValue = CBCTHs.GetBCTH(myBCTH).NAME
        'chkTinhTrang.Checked = IIf(myBCTH <> String.Empty, True, False)
        If myBCTH >= 0 Then
            chkTinhTrang.Checked = True
            cmbTinhTrang.SelectedValue = CType([Enum].Parse(GetType(enumBCTH), myBCTH), enumBCTH) 'myBCTH
        Else
            chkTinhTrang.Checked = False
            cmbTinhTrang.Enabled = False
        End If

        chkTinhTrang_CheckedChanged(Nothing, Nothing)
        'Me.cmbTinhTrang.Items.Clear()
        'chkTinhTrang_CheckedChanged(Nothing, Nothing)
        'For Each objTinhTrangPhieu As CBCTH In arys
        '    Me.cmbTinhTrang.Items.Add(objTinhTrangPhieu)
        '    i += 1
        '    If objTinhTrangPhieu.BCTH_ID = myBCTH Then
        '        chkTinhTrang.CheckState = CheckState.Checked
        '        Me.cmbTinhTrang.SelectedIndex = i
        '    End If
        'Next

        If myNGAY <> DateNull() Then
            chkGiaTri.Checked = True
            dtGiaTri.Value = myNGAY
            dtGiaTriTo.Value = myNGAYTo
        Else
            chkGiaTri.Checked = False
            dtGiaTri.Enabled = False
            dtGiaTriTo.Enabled = False
        End If
    End Sub

    Private Function ChkValue(ByVal pLoaiKho_ID As String) As Boolean
        Dim kq As Boolean = False
        If myLK.Length > 0 Then
            Dim data() As String = myLK.Split(";"c)
            If data.GetUpperBound(0) > -1 Then
                For i As Integer = 0 To data.GetUpperBound(0)
                    If pLoaiKho_ID = data(i) Then
                        kq = True
                        Exit For
                    End If
                Next
            Else
                If pLoaiKho_ID = myLK Then
                    kq = True
                End If
            End If
        End If
        Return kq
    End Function

    Private Sub btnLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoc.Click
        PopulateDK()
        SaveDK()
        Me.Close()
    End Sub

    Public Sub PopulateDK()
        'Lay DK loai kho
        myNX = ""
        myLoaiMH = ""
        mySqlDK = ""
        myShowDK = ""
        myLK = ""
        mySoPhieu = ""
        myDVGN = ""
        myTCNX = ""
        myHopDong = -1
        myKyQT = -1
        myBCTH = -1
        'myTCSL = False
        myNGAY = DateNull()
        myNGAYTo = DateNull()

        If Me.lbxLoaiKho.CheckedItems.Count > 0 Then
            Dim itemChecked As Object
            For Each itemChecked In Me.lbxLoaiKho.CheckedItems
                Dim objItem As CDoiTuong = itemChecked
                If Len(mySqlDK) > 0 Then
                    mySqlDK += " OR LK = " + objItem.KH + ""
                    myShowDK += " hoặc LK = " + objItem.Ten + ""
                    myLK += ";" + objItem.KH
                Else
                    mySqlDK = "(LK = " + objItem.KH + ""
                    myShowDK = "(LK = " + objItem.Ten + ""
                    myLK = objItem.KH
                End If
            Next
            mySqlDK += ")"
            myShowDK += ")"
        End If

        If rdoPhieuNhap.Checked = True Then
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and NX = 'N'"
                myShowDK += " và NX = N"
            Else
                mySqlDK = " NX = 'N'"
                myShowDK = " NX = N"
            End If
            myNX = "N"
        ElseIf rdoPhieuXuat.Checked = True Then
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and NX = 'X'"
                myShowDK += " và NX = X"
            Else
                mySqlDK = " NX = 'X'"
                myShowDK = " NX = X"
            End If
            myNX = "X"
        End If

        If chkLoaiMH.Checked = True Then
            Dim objLoaiMH As CLoaiThuoc
            objLoaiMH = CType(cmbLoaiMH.SelectedItem, CLoaiThuoc)
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and LoaiMH = '" + objLoaiMH.MaLoaiThuoc + "'"
                myShowDK += " và (LoaiMH = " + objLoaiMH.TenLoaiThuoc + ")"
            Else
                mySqlDK = "LoaiMH = '" + objLoaiMH.MaLoaiThuoc + "'"
                myShowDK = "LoaiMH = " + objLoaiMH.TenLoaiThuoc
            End If
            myLoaiMH = objLoaiMH.MaLoaiThuoc
        End If

        'Lay SOPHIEU
        If txtSoPhieu.Text <> "" Then
            mySoPhieu = txtSoPhieu.Text
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and SOPHIEU like N'" + mySoPhieu + "'"
                myShowDK += " và (SOPHIEU like " + txtSoPhieu.Text + ")"
            Else
                mySqlDK = "SOPHIEU like N'" + mySoPhieu + "'"
                myShowDK = "SOPHIEU like " + txtSoPhieu.Text
            End If
        End If

        'Lay DK DVGN
        If txtDVGN.Text <> "" Then
            myDVGN = CDoiTuongs.GetDoiTuongByTen(enumDOITUONG.DONVI, txtDVGN.Text).KH
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and DVGN = '" + myDVGN + "'"
                myShowDK += " và (DVGN = " + txtDVGN.Text + ")"
            Else
                mySqlDK = "DVGN = '" + myDVGN + "'"
                myShowDK = "DVGN = " + txtDVGN.Text
            End If
        End If

        If txtTCN.Text <> "" Then
            myTCNX = CDoiTuongs.GetDoiTuongByTen(enumDOITUONG.TINHCHATNHAP, txtTCN.Text).KH
            If Len(mySqlDK) > 0 Then
                If txtTCX.Text <> "" Then 'neu txtTCX <> "" thi them dau "(" de group dk
                    mySqlDK += " and (TCNX = '" + myTCNX + "'"
                    myShowDK += " và ((TCN = " + txtTCN.Text + ")"
                Else
                    mySqlDK += " and TCNX = '" + myTCNX + "'"
                    myShowDK += " và (TCN = " + txtTCN.Text + ")"
                    myTCNX = myTCNX + ";-1"
                End If
            Else
                If txtTCX.Text <> "" Then
                    mySqlDK = "(TCNX = '" + myTCNX + "'"
                    myShowDK = "(TCN = " + txtTCN.Text
                Else
                    mySqlDK = "(TCNX = '" + myTCNX + "')"
                    myShowDK = "(TCN = " + txtTCN.Text + ")"
                    myTCNX = myTCNX + ";-1"
                End If
            End If
        End If

        'Lay Tinh chat xuat
        If txtTCX.Text <> "" Then
            'kiem tra xem myTCNX da co TCN chua
            If myTCNX <> "" Then ' vi myTCNX <> "" => mySqlDK <> ""
                mySqlDK += " or TCNX = '" + CDoiTuongs.GetDoiTuongByTen(enumDOITUONG.TINHCHATXUAT, txtTCX.Text).KH + "')"
                myShowDK += " hoặc TCX = " + txtTCX.Text + ")"
                myTCNX = myTCNX + ";" + CDoiTuongs.GetDoiTuongByTen(enumDOITUONG.TINHCHATXUAT, txtTCX.Text).KH
            Else
                myTCNX = CDoiTuongs.GetDoiTuongByTen(enumDOITUONG.TINHCHATXUAT, txtTCX.Text).KH
                If Len(mySqlDK) > 0 Then
                    mySqlDK += " and (TCNX = '" + myTCNX + "')"
                    myShowDK += " và (TCX = " + txtTCX.Text + ")"
                Else
                    mySqlDK = "(TCNX = '" + myTCNX + "')"
                    myShowDK = "(TCX = " + txtTCX.Text + ")"
                End If
                myTCNX = "-1;" + myTCNX
            End If
        End If

        ''Lay Tinh trang
        'If chkTinhTrang.Checked = True And cmbTinhTrang.SelectedIndex >= 0 Then
        '    Dim objTinhTrang As CBCTH
        '    objTinhTrang = cmbTinhTrang.SelectedItem
        '    If Len(mySqlDK) > 0 Then
        '        mySqlDK += " and "
        '        myShowDK += " và "
        '    End If
        '    'Filter Tinh Trang
        '    'Chưa xác nhận (KH)
        '    If cmbTinhTrang.SelectedIndex = 0 Then
        '        mySqlDK += "(BCTH = " + objTinhTrang.BCTH.ToString() + " and TCSL = " + IIf(objTinhTrang.TCSL, 1, 0).ToString() + ")"
        '        myShowDK += "BCTH = " + objTinhTrang.NAME
        '    ElseIf cmbTinhTrang.SelectedIndex = 1 Then 'Đã xác nhận số lượng (TCSL)
        '        mySqlDK += "TCSL = " + IIf(objTinhTrang.TCSL, 1, 0).ToString()
        '        myShowDK += "BCTH = " + objTinhTrang.NAME
        '    ElseIf cmbTinhTrang.SelectedIndex = 2 Then 'Đã xác nhận tiền (TCT)
        '        mySqlDK += "BCTH = " + objTinhTrang.BCTH.ToString()
        '        myShowDK += "BCTH = " + objTinhTrang.NAME
        '    Else 'Đã xác nhận tiền (TCT) và số lượng (TCSL)
        '        mySqlDK += "(BCTH = " + objTinhTrang.BCTH.ToString() + " and TCSL = " + IIf(objTinhTrang.TCSL, 1, 0).ToString() + ")"
        '        myShowDK += "BCTH = " + objTinhTrang.NAME
        '    End If

        '    'Save DK
        '    myBCTH = objTinhTrang.NAME
        '    'myTCSL = objTinhTrang.TCSL
        'End If

        'Lay Tinh trang
        If chkTinhTrang.Checked = True And cmbTinhTrang.SelectedIndex >= 0 Then
            Dim objTinhTrang As CBCTH
            objTinhTrang = cmbTinhTrang.SelectedItem
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and "
                myShowDK += " và "
            End If
            'Filter Tinh Trang
            mySqlDK += "(BCTH = " + CType([Enum].Parse(GetType(enumBCTH), objTinhTrang.BCTH), Integer).ToString() + ")"
            myShowDK += "BCTH = " + [Enum].GetName(GetType(enumBCTH), objTinhTrang.BCTH)
            'Save DK
            myBCTH = objTinhTrang.BCTH
            'myTCSL = objTinhTrang.TCSL
        End If

        'Lay Hop Dong
        If txtSoHD.Text <> "" Then
            myHopDong = CHopDongs.GetHopDong_IDBySoHD(txtSoHD.Text)
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and HOPDONG = " + myHopDong.ToString()
                myShowDK += " và (SoHD = " + txtSoHD.Text + ")"
            Else
                mySqlDK = "HOPDONG = " + myHopDong.ToString()
                myShowDK = "SoHD = " + txtSoHD.Text
            End If
        End If

        'Lay KyQT
        If chkKyQT.Checked = True Then
            myKyQT = cmbKyQT.SelectedValue
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and KyQT = " + myKyQT.ToString()
                myShowDK += " và (KyQT = " + cmbKyQT.Text + ")"
            Else
                mySqlDK = "KyQT = " + myKyQT.ToString()
                myShowDK = "KyQT = " + cmbKyQT.Text
            End If
        End If

        If chkGiaTri.Checked = True Then
            If Len(mySqlDK) > 0 Then
                mySqlDK += " and (NGAY >= '" + Format(dtGiaTri.Value.Date, "M/d/yyyy") + "' and NGAY <= '" + Format(dtGiaTriTo.Value.Date, "M/d/yyyy") + "')"
                myShowDK += " và (NGAY >= '" + dtGiaTri.Value.ToShortDateString() + "' và NGAY <= '" + dtGiaTriTo.Value.ToShortDateString() + "')"
            Else
                mySqlDK = "NGAY >= '" + Format(dtGiaTri.Value.Date, "M/d/yyyy") + "' and NGAY <= '" + Format(dtGiaTriTo.Value.Date, "M/d/yyyy") + "'"
                myShowDK = "NGAY >= '" + dtGiaTri.Value.ToShortDateString() + "' và NGAY <= '" + dtGiaTriTo.Value.ToShortDateString() + "'"
            End If
            myNGAY = dtGiaTri.Value.Date
            myNGAYTo = dtGiaTriTo.Value.Date
        End If
    End Sub

    Private Sub LoadDK()
        myNX = CSysVars.GetGiaTri("NX")
        myLoaiMH = CSysVars.GetGiaTri("LoaiMH")
        myLK = CSysVars.GetGiaTri("LK")
        mySoPhieu = CSysVars.GetGiaTri("SoPhieu")
        myDVGN = CSysVars.GetGiaTri("DVGN")
        myTCNX = CSysVars.GetGiaTri("TCNX")
        Try
            myHopDong = CInt(CSysVars.GetGiaTri("HopDong"))
        Catch ex As Exception
            myHopDong = -1
        End Try
        Try
            myKyQT = CInt(CSysVars.GetGiaTri("KyQT"))
        Catch ex As Exception
            myKyQT = -1
        End Try
        Try
            myBCTH = CSysVars.GetGiaTri("BCTH")
        Catch ex As Exception
            myBCTH = -1
        End Try

        'Try
        '    myTCSL = CType(CSysVars.GetGiaTri("TCSL"), Boolean)
        'Catch ex As Exception
        '    myTCSL = False
        'End Try

        myNGAY = CSysVars.GetGiaTriDateTime("NGAYPH")
        myNGAYTo = CSysVars.GetGiaTriDateTime("NGAYPHTo")
    End Sub

    Private Sub SaveDK()
        CSysVars.SetGiaTri("NX", myNX)
        CSysVars.SetGiaTri("LoaiMH", myLoaiMH)
        CSysVars.SetGiaTri("LK", myLK)
        CSysVars.SetGiaTri("SoPhieu", mySoPhieu)
        CSysVars.SetGiaTri("DVGN", myDVGN)
        CSysVars.SetGiaTri("TCNX", myTCNX)
        CSysVars.SetGiaTri("HopDong", myHopDong)
        CSysVars.SetGiaTri("KyQT", myKyQT)
        CSysVars.SetGiaTri("BCTH", myBCTH)
        'CSysVars.SetGiaTri("TCSL", myTCSL)
        CSysVars.SetGiaTriDateTime("NGAYPH", myNGAY)
        CSysVars.SetGiaTriDateTime("NGAYPHTo", myNGAYTo)
    End Sub

    Private Sub chkLoaiMH_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLoaiMH.CheckedChanged
        If chkLoaiMH.Checked Then
            cmbLoaiMH.Enabled = True
            cmbLoaiMH.Text = CLoaiThuocs.GetLoaiThuocByMa(myLoaiMH).TenLoaiThuoc
        Else
            cmbLoaiMH.Enabled = False
            cmbLoaiMH.Text = ""
        End If
    End Sub

    Private Sub chkTinhTrang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTinhTrang.CheckedChanged
        If chkTinhTrang.Checked Then
            cmbTinhTrang.Enabled = True
            'If myBCTH <> "" Then
            '    cmbTinhTrang.SelectedValue = CBCTHs.GetBCTH(myBCTH).NAME
            'Else
            '    cmbTinhTrang.SelectedIndex = 0
            'End If
            'cmbTinhTrang.SelectedValue = myBCTH
            cmbTinhTrang.SelectedValue = CType([Enum].Parse(GetType(enumBCTH), myBCTH), enumBCTH) 'myBCTH

            'cmbTinhTrang.Text = CBCTHs.GetBCTH(myBCTH).NAME
        Else
            cmbTinhTrang.Enabled = False
            cmbTinhTrang.Text = ""
        End If
    End Sub

    Private Sub txtSoHD_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSoHD.DoubleClick
        Dim frm As New frmHopDong
        frm.ShowDialog()
        If frm.selectedHD IsNot Nothing Then
            txtSoHD.Text = frm.selectedHD.SOHOPDONG
        Else
            txtSoHD.Text = ""
        End If
    End Sub

    'To mau cho control khi Focus
    Private Sub textBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles txtSoPhieu.Enter, txtDVGN.Enter, txtTCN.Enter, txtTCX.Enter, txtSoHD.Enter
        Try
            SetColorAllControls(sender, True)
            If (CType(sender, TextBox) Is txtDVGN) Or _
                (CType(sender, TextBox) Is txtTCN) Or _
                (CType(sender, TextBox) Is txtTCX) Or _
                (CType(sender, TextBox) Is txtSoHD) Then
                ToolStripStatusLabel1.Text = "Nhấn F1 hoặc đúp chuột để chọn"
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub txtSoHD_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSoHD.KeyDown
        If e.KeyCode = Keys.F1 Then
            txtSoHD_DoubleClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub textBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles txtSoPhieu.Leave, txtDVGN.Leave, txtTCN.Leave, txtTCX.Leave, txtSoHD.Leave
        SetColorAllControls(sender, False)
        ToolStripStatusLabel1.Text = ""
    End Sub


    Private Sub chkGiaTri_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGiaTri.CheckedChanged
        dtGiaTri.Enabled = chkGiaTri.Checked
        dtGiaTriTo.Enabled = chkGiaTri.Checked
    End Sub

    Public Sub ShowFormDoiTuong(ByVal loai As enumDOITUONG, ByVal value As String)
        Dim frm As New frmDMDoiTuong
        frm.bCallForm = True
        frm.eDoiTuong = loai
        frm.sFind = value
        frm.ShowDialog()
        If frm.selectedDT IsNot Nothing Then
            Select Case loai
                Case enumDOITUONG.DONVI
                    txtDVGN.Text = frm.selectedDT.Ten
                Case enumDOITUONG.TINHCHATNHAP
                    txtTCN.Text = frm.selectedDT.Ten
                Case enumDOITUONG.TINHCHATXUAT
                    txtTCX.Text = frm.selectedDT.Ten
            End Select
        Else
            Select Case loai
                Case enumDOITUONG.DONVI
                    txtDVGN.Text = ""
                Case enumDOITUONG.TINHCHATNHAP
                    txtTCN.Text = ""
                Case enumDOITUONG.TINHCHATXUAT
                    txtTCX.Text = ""
            End Select
        End If
    End Sub

    Private Sub txtDoiTuong_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDVGN.DoubleClick, txtTCN.DoubleClick, txtTCX.DoubleClick
        If sender Is txtDVGN Then
            ShowFormDoiTuong(enumDOITUONG.DONVI, txtDVGN.Text)
        ElseIf sender Is txtTCN Then
            ShowFormDoiTuong(enumDOITUONG.TINHCHATNHAP, txtTCN.Text)
        Else
            ShowFormDoiTuong(enumDOITUONG.TINHCHATXUAT, txtTCX.Text)
        End If
    End Sub

    Private Sub txtDoiTuong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDVGN.KeyDown, txtTCN.KeyDown, txtTCX.KeyDown
        If e.KeyCode = Keys.F1 Then
            If sender Is txtDVGN Then
                ShowFormDoiTuong(enumDOITUONG.DONVI, txtDVGN.Text)
            ElseIf sender Is txtTCN Then
                ShowFormDoiTuong(enumDOITUONG.TINHCHATNHAP, txtTCN.Text)
            Else
                ShowFormDoiTuong(enumDOITUONG.TINHCHATXUAT, txtTCX.Text)
            End If
        End If
    End Sub

    Private Sub txtDoiTuong_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDVGN.Leave, txtTCN.Leave, txtTCX.Leave
        Dim loai As enumDOITUONG
        If sender Is txtDVGN Then
            loai = enumDOITUONG.DONVI
        ElseIf sender Is txtTCN Then
            loai = enumDOITUONG.TINHCHATNHAP
        Else
            loai = enumDOITUONG.TINHCHATXUAT
        End If
        Dim strTen As String = CType(sender, TextBox).Text

        If strTen <> "" Then
            Dim strSql As String = "select * from tblDMKhac where TEN like N'" + strTen + "' and L = '" + LocDoiTuong(loai) + "'"
            Dim arrFind As ArrayList = CDoiTuongs.GetDoiTuongListBySql(strSql)
            If arrFind.Count = 0 Then
                ShowFormDoiTuong(loai, strTen)
            End If
        End If
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Huy()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub txtSoHD_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSoHD.Leave
        Dim strTen As String = txtSoHD.Text
        If strTen <> "" Then
            Dim strSql As String = "sohopdong like N'" + strTen + "'"
            Dim arrFind As ArrayList = CHopDongs.GetHopDongList(strSql)
            If arrFind.Count = 0 Then
                Dim frm As New frmHopDong
                frm.ShowDialog()
                If frm.selectedHD IsNot Nothing Then
                    txtSoHD.Text = frm.selectedHD.SOHOPDONG
                Else
                    txtSoHD.Text = ""
                End If
            End If
        End If
    End Sub

    Private Sub chkKyQT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkKyQT.CheckedChanged
        cmbKyQT.Enabled = chkKyQT.Checked
        cmbKyQT.SelectedValue = 1
    End Sub

    Private Sub cmbTinhTrang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTinhTrang.SelectedIndexChanged

    End Sub
End Class